package com.cqie.util;

import com.cqie.constant.ResultCodeEnum;
import lombok.Data;

@Data
public final class Result<T> {
    private int code;
    private String message;
    private T data;

    public Result() {

    }

    public Result(int code) {
        this.code = code;
        this.message = "";
        this.data = null;
    }

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public static Result Success(Object data) {
        return new Result(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), data);
    }

    public static Result Success(String message, Object data) {
        return new Result(ResultCodeEnum.SUCCESS.getCode(), message, data);
    }

    public static Result Success(Integer code, String message) {
        return new Result(code,message);
    }

    public static Result Success() {
        return Success("");
    }

    public static Result Failed(String message) {
        return new Result(ResultCodeEnum.SYSTEM_EXCEPTION.getCode(), message);
    }

    public static Result Failed() {
        return Failed("Failed");
    }

//    public static R Failed(int code, String message) {
//        return new R(code, message);
//    }

    public static Result Failed(Integer code , String message) {
        return new Result(code, message);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String msg) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean succeeded() {
        return getCode() == ResultCodeEnum.SUCCESS.getCode();
    }


    public boolean failed() {
        return getCode() != ResultCodeEnum.SUCCESS.getCode();
    }

}